﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;

namespace SteveDower.SimpleBacklog
{
    public class DropDownButton : CheckBox
    {
        public ContextMenu DropDown
        {
            get { return (ContextMenu)GetValue(DropDownProperty); }
            set { SetValue(DropDownProperty, value); }
        }

        // Using a DependencyProperty as the backing store for DropDown.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DropDownProperty =
            DependencyProperty.Register("DropDown", typeof(ContextMenu), typeof(DropDownButton), new UIPropertyMetadata(null));

        protected override void OnClick()
        {
            if (DropDown != null)
            {
                DropDown.PlacementTarget = this;
                DropDown.Placement = PlacementMode.Bottom;

                DropDown.Closed += new RoutedEventHandler(DropDown_Closed);
                DropDown.IsOpen = true;
                DropDown.Focus();
            }
            
            base.OnClick();
        }

        void DropDown_Closed(object sender, RoutedEventArgs e)
        {
            var cm = sender as ContextMenu;
            if (cm == null) return;

            cm.Closed -= DropDown_Closed;
            IsChecked = false;
        }
    }
}
